<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class FormView {
	
	var $_script	=	'';
	var $_event		=	'';
	var $_html		= 	'';
	
	var $form_id	=	'';
	var $form_action=	'';
	var $form_bean	=	'';
	var $form_data	=	'';
	
	var $table_class	=	'formTable';
	
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 */
	public function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->initialize($params);
		}

		log_message('debug', "FormView Class Initialized");
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize Preferences
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}

	// --------------------------------------------------------------------
	
	function getFormItem($item_item,$item_id,$item_name,$item_value) {
		$res = '';
		$item_items = explode(':',$item_item,2);
		$item_item = $item_items[0];
		$item_item_attr = count($item_items)==1?'':$item_items[1];
		
		switch ($item_item) {
			case 'hidden':
			case 'text':
				$res = '<input '.$item_item_attr.' type="'.$item_item.'" id="'.$item_id.'" name="'.$item_name.'" value="'.$item_value.'" />';
		       	break;
			case 'submit':
			case 'button':
			case 'reset':
				$res = '<input '.$item_item_attr.' type="'.$item_item.'" '.($item_id?"id=$item_id":'').' value="'.$item_value.'" />';
		       	break;
			case 'label':
				$res = '<label '.$item_item_attr.' id="'.$item_id.'">'.$item_value.'</label> ';
				break;
			case 'editor':
			case 'textarea':
				$res = '<textarea '.$item_item_attr.' id="'.$item_id.'" name="'.$item_name.'" cols="60">'.$item_value.'</textarea>';
				break;
			case 'radio':
				$item_item_attr = json_decode($item_item_attr);
				$checked = '';
				foreach ($item_item_attr as $key=>$value) {
					if($key == $item_value){
						$checked = 'checked';
					}
					$res .= '<input type=radio name='.$item_name.' value='.$key.' '.$checked.'/>'.$value;
				}
				break;
			case 'upfile':
				
				break;
		}
		return $res;
	}
	/*
	function getJSEvent($filed,$this->_events) {
		$binds = "";
		foreach ($this->_events as $this->_event => $fn) {
			//$binds .= bind(document.getElementById("$filed"),"$this->_event",$fn);
			
			$binds .= "$('#".$filed."').".$this->_event."($fn)";
		}
		return $binds;
	}*/
	
	function setForm($form_bean) {
		
		foreach ($form_bean as $item) {
			
			$item_label	= isset($item['label']) ? $item['label'] : NULL;
			$item_field	= isset($item['field']) ? $item['field'] : NULL;
			$item_item	= isset($item['item']) ? $item['item'] : NULL;//item类型
			$item_value = isset($item['value']) ? $item['value'] : NULL;
			$item_rules = isset($item['rules']) ? $item['rules'] : NULL;
			$item_msg	= isset($item['msg']) ? $item['msg'] : NULL;
			$item_event	= isset($item['event']) ? $item['event'] : NULL;//数组
			
			$item_id	= $item_field;
			$item_name	= $item_field;
			$item_bean_name = NULL;
			
			if ($item_field) {
				if(strpos($item_id,'[')){
					$item_bean_name_s = explode('[',$item_id,2);
					$item_bean_name	= $item_bean_name_s[0];
					$item_id = str_replace(']','_',str_replace('[','_',$item_bean_name_s[1]));
					$item_id = $item_bean_name.'_'.substr($item_id,0,strlen($item_id)-1);
					$item_field = substr($item_bean_name_s[1],0,strlen($item_bean_name_s[1])-1);
				}
				
				if ($this->form_data) {
					if(isset($this->form_data[$item_bean_name][$item_field]))
						$item_value = $this->form_data[$item_bean_name][$item_field];
					else if(isset($this->form_data[$item_field]))
						$item_value = $this->form_data[$item_field];
				}
				
				if ($item_rules) {
					if ( strpos($item_rules,'is_exist:') > -1 && $item_value)
						$item_rules = str_replace('is_exist:','is_exist:'.$item_value.'@',$item_rules);
					$this->_script .= $item_id.':\''.$item_rules.'\',';
				}
			}
		
			if ($item_event) {
				$this->_event .= $item_event;
			}
			
			if (isset($item_label)) {
				if ($item_id) {
					$this->_html .= '<tr><th width="150"><label for="'.$item_id.'"><tt id="'.$item_id.'_name">'.$item_label.'</tt></label>：</th><td>';
				} else if ($item_label == ''){
					$this->_html .= '<tr><td width="150">&nbsp;</td><td>';
				} else{
					$this->_html .= '<tr><th width="150"><label>'.$item_label.'</label>：</th><td>';
				}
			}
			
			if ($item_item) {
				$this->_html .= $this->getFormItem($item_item,$item_id,$item_name,$item_value);
			}else if (isset($item['items'])){
				$this->setForm($item['items']);
			}
			
			if ($item_id && $item_msg) {
				$this->_html .= '<div id="'.$item_id.'_tip">'.$item_msg.'</div>';
			}
			
			if (isset($item_label)) {
				$this->_html .= '</td></tr>';
			}
		}
	}
	
	function getForm($form_id='',$form_action='',$form_bean='',$form_data=''){
		if($form_id && $form_action && $form_bean){
			$this->form_id	=	$form_id;
			$this->form_action	=	$form_action;
			$this->form_bean	=	$form_bean;
			$this->form_data	=	$form_data;
			
			$this->_script	=	'<script>new plugor.form(\''.$this->form_id.'\',\''.$this->form_action.'\',{';
			$this->_html	= 	'<form id="'.$this->form_id.'" name="'.$this->form_id.'" method="post">
				<table class="'.$this->table_class.'" border="0" cellpadding="0" cellspacing="0">';
			
			$this->setForm($this->form_bean);
			
			$this->_script	=	substr($this->_script,0,strlen($this->_script)-1).'});';
			$this->_event	.=	'</script>';
			$this->_html	.=	'</table></form>';
		
			return $this->_html.$this->_script.$this->_event;
		}else{
			return 'FormView 缺少参数！';
		}
	}
	
}
?>